/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nuce.bo;

import dataaccess.DataAccess;
import java.sql.*;
import java.util.List;
import java.util.Queue;
import java.util.logging.Level;
import java.util.logging.Logger;
import nuce.entity.Question;
import nuce.entity.User;

/**
 *
 * @author LINHNC
 */
public class QuestionSet {

    private Question[] item;
    private int n;

    public QuestionSet() {
        n = 0;
        item = null;
    }

    public Question getItem(int i) {
        if (i<n && n>0)
            return item[i];
        else return null;
    }

    public int getCount() {
        return n;
    }
    //tham cau hoi vao tap hop

    public void add(Question q) {
        if (q != null) {
            Question[] b = new Question[n + 1];
            int i;
            for (i = 0; i < n; i++) {
                b[i] = new Question();
                b[i] = item[i];
            }
            b[n] = new Question();
            b[n] = q;
            item = new Question[n + 1];
            n++;
            for (i = 0; i < n; i++) {
                item[i] = new Question();
                item[i] = b[i];
            }
        }
    }

    public void remove(int index) {
        if (index >= 0 && index < n) {
            for (int i = index; i < n - 1; i++) {
                item[i] = item[i + 1];
            }
            n--;
        }
    }
    public void SelectByLevel(int level) {
        final String procName = "Question_SelectByLevel";
        final String procNameCount = "Question_SelectCountByLevel";

        DataAccess da = new DataAccess();
        String[] args = new String[1];
        args[0] = Integer.toString(level);
        ResultSet rs = da.executeProc(procNameCount, args);
        if (rs != null) {
            try {
                rs.next();
                this.n = Integer.parseInt(rs.getString(1));
                item = new Question[n];
                rs = da.executeProc(procName, args);
                int i = 0;
                while (rs.next()) {
                    item[i] = new Question();
                    item[i].setContent(rs.getString("content"));
                    item[i].setAnswer1(rs.getString("answer1"));
                    item[i].setAnswer2(rs.getString("answer2"));
                    item[i].setAnswer3(rs.getString("answer3"));
                    item[i].setAnswer4(rs.getString("answer4"));
                    item[i].setLevel(Integer.parseInt(rs.getString("level")));
                    i++;
                }


            } catch (SQLException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);

            }
        }
    }
    public void updateItem(int i,Question q){ //gan item[i] = q ; q is new values of Question
        Question qTemp = new Question(q);
        if (i>=0 && i<n){
            item[i] = qTemp;
        }
    }
}
